<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>彩色留言板</title>
    <style>
        .container{
            width: 350px;
            height: 300px;
            text-align: center;
            margin: 0 auto;
        }
        .grey{
            color: grey;
        }
        .container .row{
            width: 350px;
            height: 40px;
            display: flex;
            justify-content: space-between;
            margin: 10px 0;
        }
        .container .row input{
            width: 260px;
            height: 30px;
        }
        #submit {
            width: 350px;
            height: 40px;
            background-color: orange;
            color: white;
            border: none;
            margin: 10px;
            border-radius: 5px;
            font-size: 20px;
            cursor: pointer;
        }
        #messages {
            width: 350px;
            margin: 20px auto;
            text-align: center;
        }
        .message {
            margin: 5px 0;
            padding: 8px;
            background-color: #f0f0f0;
            border-radius: 4px;
            transition: all 0.3s ease;
        }
        .message:hover {
            transform: translateX(5px);
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>表白墙</h1>
        <p class="grey">输入后点击提交，会将信息显示在下方</p>
        <div class="row">
            <span>谁:</span><input type="text" id="from">
        </div>
        <div class="row">
            <span>对谁:</span><input type="text" id="to">
        </div>
        <div class="row">
            <span>说什么:</span><input type="text" id="say">
        </div>
        <input type="button" value="提交" id="submit" onclick="addMessage()">
    </div>
    <div id="messages"></div>
    
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
    <script>
        // 方法1: 随机颜色
        function getRandomColor() {
            // 生成明亮的颜色，确保文字可读性
            const letters = '3456789ABCDEF';
            let color = '#';
            for (let i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 12)];
            }
            return color;
        }
        
        // 方法2: 交替颜色
        let messageCount = 0;
        function getAlternatingColor() {
            const colors = ['#2c3e50', '#e74c3c', '#3498db', '#2ecc71'];
            return colors[messageCount % colors.length];
        }
        
        function addMessage(){
            let from=$("#from").val();
            let to=$("#to").val();
            let say=$("#say").val();
            
            if(from.trim()===""||to.trim()===""||say.trim()===""){
                alert("请填写完整信息");
                return;
            }
            
            // 选择一种颜色方案
            const textColor = getRandomColor();
            // 或者使用交替颜色
            // const textColor = getAlternatingColor();
            
            // 创建带颜色的留言元素
            let html = `<div class="message" style="color: ${textColor}">` + 
                      `${from} 对 ${to} 说：${say}</div>`;
            
            $("#messages").append(html);
            messageCount++; // 用于交替颜色计数
            
            // 清空输入框
            // $("#from").val("");
            // $("#to").val("");
            // $("#say").val("");
        }
    </script>
</body>
</html>

